package com.jie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.dao.BlogATagMapper;
import com.jie.pojo.Blog;
import com.jie.pojo.Blog_Tag;
import com.jie.pojo.Tag;
import com.jie.pojo.Type;
import com.jie.service.BlogService;
import com.jie.service.TagsService;
import com.jie.service.TypeService;
import com.jie.service.UserService;
import com.jie.utils.MarkdownUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/15/12:10
 * @Description:
 */
@Controller
public class IndexController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagsService tagsService;
    @Autowired
    private UserService userService;
    @Autowired
    private BlogATagMapper blogATagMapper;
    @GetMapping("/")
    public String index(@RequestParam(defaultValue = "1",value = "page") int page,
                        @RequestParam(defaultValue = "8",value = "pageSize") Integer pageSize,
                        Model model){
        //构造分页
        Page<Blog> pageInfo =new Page<>(page,pageSize);
        LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Blog::getUpdateTime);
        Page<Blog> pages = blogService.page(pageInfo,queryWrapper);
        for (Blog record : pages.getRecords()) {
            record.setUser(userService.list().get(0));
            record.setTypes(typeService.getById(record.getType()));
        }
        model.addAttribute("page",pages);
        //type
        Page<Type> page1=new Page<>(1,10);
        Page<Type> page2 = typeService.page(page1, null);
        for (Type record : page2.getRecords()) {
            LambdaQueryWrapper<Blog> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(Blog::getType,record.getId());
            record.setBlogList(blogService.list(queryWrapper1));
        }
        model.addAttribute("types",page2);
        //tag
        Page<Tag> page3=new Page<>(1,10);
        Page<Tag> page4 = tagsService.page(page3, null);
        for (Tag record : page4.getRecords()) {
            LambdaQueryWrapper<Blog_Tag> queryWrapper2=new LambdaQueryWrapper<>();
            queryWrapper2.eq(Blog_Tag::getTId,record.getId());
            record.setBlogList(blogATagMapper.selectList(queryWrapper2));
        }
        model.addAttribute("tags",page4);
        //recommend
        Page<Blog> page5=new Page<>(1,10);
        LambdaQueryWrapper<Blog> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Blog::getRecommend,1);
        lambdaQueryWrapper.orderByDesc(Blog::getUpdateTime);
        Page<Blog> page6 = blogService.page(page5, lambdaQueryWrapper);
        model.addAttribute("recommend",page6);
        return "index";
    }

    @GetMapping("/search")
    public String search(@RequestParam(defaultValue = "1",value = "page") int page,
                         @RequestParam(defaultValue = "8",value = "pageSize") Integer pageSize,
                         String query,Model model){
        LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(Blog::getTitle,query).or().like(Blog::getContent,query).or().like(Blog::getDescription,query);
        queryWrapper.orderByDesc(Blog::getUpdateTime);
        Page<Blog> blogPage=new Page<>(page,pageSize);
        Page<Blog> page1 = blogService.page(blogPage, queryWrapper);
        for (Blog record : page1.getRecords()) {
            record.setUser(userService.list().get(0));
            record.setTypes(typeService.getById(record.getType()));
        }
        model.addAttribute("page",page1);
        model.addAttribute("query",query);
        return "search";
    }
    @Transactional
    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id,Model model){
        Blog byId = blogService.getById(id);
        if(byId.getFlag()==0){
            byId.setFlags("原创");
        }
        if(byId.getFlag()==2){
            byId.setFlags("转载");
        }
        if(byId.getFlag()==3){
            byId.setFlags("翻译");
        }
        byId.setUser(userService.list().get(0));
        //标签
        LambdaQueryWrapper<Blog_Tag> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Blog_Tag::getBId,byId.getId());
        List<Blog_Tag> blogTagList = blogATagMapper.selectList(lambdaQueryWrapper);
        List<Tag> tagList=new ArrayList<>();
        for (Blog_Tag blog_tag : blogTagList) {
            tagList.add(tagsService.getById(blog_tag.getTId()));
        }
        byId.setTagList(tagList);

        Blog blog=new Blog();//处理content防止数据库变化
        BeanUtils.copyProperties(byId,blog);
        String content = blog.getContent();
        blog.setContent(MarkdownUtil.markdownToHtmlExtens(content));

        //自增1
        UpdateWrapper<Blog> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",blog.getId());
        wrapper.setSql("`views`=`views`+1");
        blogService.update(wrapper);


        model.addAttribute("blog",blog);
        return "blog";
    }

    @GetMapping("/footer/newBlog")
    public String newBlogs(Model model){
        LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Blog::getUpdateTime);
        queryWrapper.last("limit 3");
        model.addAttribute("newblogs",blogService.list(queryWrapper));
        return "index :: newBlogList";
    }
}
